﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_MagazineItem")]
    public class T_MagazineItem : IEntity<T_MagazineItem>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 所属期刊
        /// </summary>
        public int PeriodicalId { get; set; }
        /// <summary>
        /// 本期刊号
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Manuscriptcode { get; set; }

        public DateTime PublishDate { get; set; }
        public DateTime CreateDate { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string CoverImage { get; set; }

        [Column(TypeName = "text")]
        public string Information { get; set; }

        public int Status { get; set; }
        /// <summary>
        /// 页面所在基础地址
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string PagePath { get; set; }

        public override T_MagazineItem Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_MagazineItem Find(System.Linq.Expressions.Expression<Func<T_MagazineItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.MagazineItem.FirstOrDefault(where);
                if (item != null)
                {
                    this.CoverImage = item.CoverImage;
                    this.CreateDate = item.CreateDate;
                    this.Id = item.Id;
                    this.Information = item.Information;
                    this.Manuscriptcode = item.Manuscriptcode;
                    this.PagePath = item.PagePath;
                    this.PeriodicalId = item.PeriodicalId;
                    this.PublishDate = item.PublishDate;
                    this.Status = item.Status;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_MagazineItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.MagazineItem.Any(where);
            }
        }
    }
}
